package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize) {

        log.info("page={},pageSize={}", page, pageSize);
        //第一步  构建分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加一个排序条件 查询   以时间来排序
        lqw.orderByAsc(Category::getSort);
        //执行查询
        categoryService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }

    @PostMapping
    public R add(@RequestBody Category category){
        log.info("添加菜品的菜品对象:{}",category);
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 修改套餐或者菜品数据
     * @param category
     * @return
     */
    @PutMapping
    public  R update(@RequestBody Category category){
        log.info("添加修改的菜品对象:{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    //删除
    @DeleteMapping
    public R delete(Long id){
        log.info("添加删除的菜品对象id:{}",id);

        //对删除进行完善  根据id去查询菜品
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

}
